# coding=utf8
import base64
from Crypto.Cipher import AES

# 补足字符串长度为16的倍数
def add_to_16(s):
    while len(s) % 16 != 0:
        s += '\0'
    return str.encode(s)  # 返回bytes
 
AES_KEY = '1234567890123456'  # 密钥长度必须为16、24或32位，分别对应AES-128、AES-192和AES-256
 
def _gen_aes():
    return AES.new(str.encode(AES_KEY), AES.MODE_ECB)

def encrypt(text):
    return str(base64.encodebytes(_gen_aes().encrypt(add_to_16(text))), encoding='utf8').replace('\n', '')
    
def decrypt(encrypted_text):
    return str(_gen_aes().decrypt(base64.decodebytes(bytes(encrypted_text, encoding='utf8'))).rstrip(b'\0').decode("utf8"))

if __name__ == "__main__":
    print("加密hello", encrypt('hello'))
    print("解密hello", decrypt(encrypt('hello')))
